package c.f.d.a.a.a.c.a;

import android.os.Build.VERSION;
import android.security.keystore.KeyGenParameterSpec.Builder;
import android.text.TextUtils;
import com.huawei.hms.analytics.core.crypto.AesCipher.AesLen;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;

public class a {
    public static final String a = "a";

    public static byte[] a(String str, byte[] bArr) {
        String str2;
        StringBuilder g;
        int i = 0;
        byte[] bArr2 = new byte[0];
        if (TextUtils.isEmpty(str)) {
            c.f.a.a.a.a.m(a, "alias or encrypt content is null");
            return bArr2;
        }
        if (VERSION.SDK_INT >= 23) {
            i = 1;
        }
        if (i == 0) {
            c.f.a.a.a.a.m(a, "sdk version is too low");
            return bArr2;
        } else if (bArr.length <= 12) {
            c.f.a.a.a.a.m(a, "Decrypt source data is invalid.");
            return bArr2;
        } else {
            SecretKey b = b(str);
            if (b == null) {
                c.f.a.a.a.a.m(a, "Decrypt secret key is null");
                return bArr2;
            }
            byte[] copyOf = Arrays.copyOf(bArr, 12);
            try {
                Cipher instance = Cipher.getInstance("AES/GCM/NoPadding");
                instance.init(2, b, new GCMParameterSpec(AesLen.ROOTKEY_COMPONET_LEN, copyOf));
                bArr2 = instance.doFinal(bArr, 12, bArr.length - 12);
            } catch (NoSuchAlgorithmException e) {
                str2 = a;
                g = c.b.a.a.a.g("NoSuchAlgorithmException : ");
                g.append(e.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (NoSuchPaddingException e2) {
                str2 = a;
                g = c.b.a.a.a.g("NoSuchPaddingException : ");
                g.append(e2.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (InvalidKeyException e3) {
                str2 = a;
                g = c.b.a.a.a.g("InvalidKeyException : ");
                g.append(e3.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (InvalidAlgorithmParameterException e4) {
                str2 = a;
                g = c.b.a.a.a.g("InvalidAlgorithmParameterException : ");
                g.append(e4.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (IllegalBlockSizeException e5) {
                str2 = a;
                g = c.b.a.a.a.g("IllegalBlockSizeException : ");
                g.append(e5.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (BadPaddingException e6) {
                str2 = a;
                g = c.b.a.a.a.g("BadPaddingException : ");
                g.append(e6.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            } catch (Exception e7) {
                str2 = a;
                g = c.b.a.a.a.g("Exception: ");
                g.append(e7.getMessage());
                c.f.a.a.a.a.m(str2, g.toString());
            }
            return bArr2;
        }
    }

    public static synchronized SecretKey b(String str) {
        SecretKey secretKey;
        String str2;
        StringBuilder stringBuilder;
        synchronized (a.class) {
            secretKey = null;
            try {
                KeyStore instance = KeyStore.getInstance("AndroidKeyStore");
                instance.load(null);
                Key key = instance.getKey(str, null);
                if (key == null || !(key instanceof SecretKey)) {
                    KeyGenerator instance2 = KeyGenerator.getInstance("AES", "AndroidKeyStore");
                    instance2.init(new Builder(str, 3).setBlockModes(new String[]{"GCM"}).setEncryptionPaddings(new String[]{"NoPadding"}).setKeySize(256).build());
                    secretKey = instance2.generateKey();
                } else {
                    secretKey = (SecretKey) key;
                }
            } catch (KeyStoreException e) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("KeyStoreException : ");
                stringBuilder.append(e.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (IOException e2) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("IOException : ");
                stringBuilder.append(e2.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (CertificateException e3) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("CertificateException : ");
                stringBuilder.append(e3.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (NoSuchAlgorithmException e4) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("NoSuchAlgorithmException : ");
                stringBuilder.append(e4.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (UnrecoverableKeyException e5) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("UnrecoverableKeyException : ");
                stringBuilder.append(e5.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (InvalidAlgorithmParameterException e6) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("InvalidAlgorithmParameterException : ");
                stringBuilder.append(e6.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (NoSuchProviderException e7) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("NoSuchProviderException : ");
                stringBuilder.append(e7.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            } catch (Exception e8) {
                str2 = a;
                stringBuilder = new StringBuilder();
                stringBuilder.append("Exception: ");
                stringBuilder.append(e8.getMessage());
                c.f.a.a.a.a.m(str2, stringBuilder.toString());
            }
        }
        return secretKey;
    }

    public static byte[] c(String str, byte[] bArr) {
        String str2;
        StringBuilder g;
        byte[] bArr2 = new byte[0];
        if (TextUtils.isEmpty(str) || bArr == null) {
            c.f.a.a.a.a.m(a, "alias or encrypt content is null");
            return bArr2;
        }
        if ((VERSION.SDK_INT >= 23 ? 1 : null) == null) {
            c.f.a.a.a.a.m(a, "sdk version is too low");
            return bArr2;
        }
        try {
            Cipher instance = Cipher.getInstance("AES/GCM/NoPadding");
            SecretKey b = b(str);
            if (b == null) {
                c.f.a.a.a.a.m(a, "Encrypt secret key is null");
                return bArr2;
            }
            instance.init(1, b);
            byte[] doFinal = instance.doFinal(bArr);
            bArr = instance.getIV();
            if (bArr != null) {
                if (bArr.length == 12) {
                    bArr2 = Arrays.copyOf(bArr, bArr.length + doFinal.length);
                    System.arraycopy(doFinal, 0, bArr2, bArr.length, doFinal.length);
                    return bArr2;
                }
            }
            c.f.a.a.a.a.m(a, "IV is invalid.");
            return bArr2;
        } catch (NoSuchAlgorithmException e) {
            str2 = a;
            g = c.b.a.a.a.g("NoSuchAlgorithmException : ");
            g.append(e.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        } catch (NoSuchPaddingException e2) {
            str2 = a;
            g = c.b.a.a.a.g("NoSuchPaddingException : ");
            g.append(e2.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        } catch (BadPaddingException e3) {
            str2 = a;
            g = c.b.a.a.a.g("BadPaddingException : ");
            g.append(e3.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        } catch (IllegalBlockSizeException e4) {
            str2 = a;
            g = c.b.a.a.a.g("IllegalBlockSizeException : ");
            g.append(e4.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        } catch (InvalidKeyException e5) {
            str2 = a;
            g = c.b.a.a.a.g("InvalidKeyException : ");
            g.append(e5.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        } catch (Exception e6) {
            str2 = a;
            g = c.b.a.a.a.g("Exception: ");
            g.append(e6.getMessage());
            c.f.a.a.a.a.m(str2, g.toString());
        }
    }
}
